var graph = argument0;
var height = array_height_2d(graph);
var width = array_length_2d(graph, 0);

//Initialization
//int[,] graphDistance = new int[height, width];
var graphDistance = 0;

//int[] start = new int[2]; //y, x
var start = 0;

for (var iii = 0; iii < height; iii++)
{
    for (var jjj = 0; jjj < width; jjj++)
    {
        graphDistance[iii, jjj] = -1;
        if (graph[iii, jjj] == 2)
        {
            start[0] = iii;
            start[1] = jjj;
            graphDistance[iii, jjj] = 0;
        }
    }
}

//Mark Distances
//Stack<int[]> neighbors = new Stack<int[]>();
var neighbors = ds_stack_create();
//neighbors.Push(start);
ds_stack_push(neighbors, start);

//Find Finish
//int maxDistance = -1;
var maxDistance = -1;

//int[] finish = new int[2];
var finish = 0;

//while (neighbors.Count != 0)
while (ds_stack_size(neighbors) != 0)
{
    //int[] current = neighbors.Pop();
    var current = 0;
    current = ds_stack_pop(neighbors);
    
    if (graphDistance[current[0], current[1]] > maxDistance)
    {
        maxDistance = graphDistance[current[0], current[1]];
        finish = 0;
        finish[0] = current[0];
        finish[1] = current[1];
    }

    if (current[1] + 1 < width &&
        graph[current[0], current[1] + 1] != 0 &&
        graphDistance[current[0], current[1] + 1] == -1)
    {
        graphDistance[current[0], current[1] + 1] = graphDistance[current[0], current[1]] + 1;
        
        var toPush = 0;
        toPush[0] = current[0];
        toPush[1] = current[1] + 1;
        
        //neighbors.Push(new int[] { current[0], current[1] + 1 });
        ds_stack_push(neighbors, toPush);        
    }
    if (current[0] - 1 > -1 &&
        graph[current[0] - 1, current[1]] != 0 &&
        graphDistance[current[0] - 1, current[1]] == -1)
    {
        graphDistance[current[0] - 1, current[1]] = graphDistance[current[0], current[1]] + 1;
        
        var toPush = 0;
        toPush[0] = current[0] - 1;
        toPush[1] = current[1];
        
        //neighbors.Push(new int[] { current[0] - 1, current[1] });
        ds_stack_push(neighbors, toPush);
    }
    if (current[1] - 1 > -1 &&
        graph[current[0], current[1] - 1] != 0 &&
        graphDistance[current[0], current[1] - 1] == -1)
    {
        graphDistance[current[0], current[1] - 1] = graphDistance[current[0], current[1]] + 1;
        
        var toPush = 0;
        toPush[0] = current[0];
        toPush[1] = current[1] - 1;
        
        //neighbors.Push(new int[] { current[0], current[1] - 1 });
        ds_stack_push(neighbors, toPush);
    }
    if (current[0] + 1 < height &&
        graph[current[0] + 1, current[1]] != 0 && 
        graphDistance[current[0] + 1, current[1]] == -1)
    {
        graphDistance[current[0] + 1, current[1]] = graphDistance[current[0], current[1]] + 1;
        
        var toPush = 0;
        toPush[0] = current[0] + 1;
        toPush[1] = current[1];
        
        //neighbors.Push(new int[] { current[0] + 1, current[1] });
        ds_stack_push(neighbors, toPush);
    }
}

graph[finish[0], finish[1]] = 3;
return graph;
